package com.siyuanren.gcocp.service.impl;

import com.siyuanren.gcocp.dto.wx.WxUserVoucherDTO;
import com.siyuanren.gcocp.entity.WxUserVoucher;
import com.siyuanren.gcocp.entity.WxUserVoucherExample;
import com.siyuanren.gcocp.entity.WxVoucher;
import com.siyuanren.gcocp.enums.wx.VoucherStateEnum;
import com.siyuanren.gcocp.mapper.MyWxUserVoucherMapper;
import com.siyuanren.gcocp.mapper.base.WxUserVoucherMapper;
import com.siyuanren.gcocp.mapper.base.WxVoucherMapper;
import com.siyuanren.gcocp.service.UserVoucherService;
import com.siyuanren.gcocp.service.VoucherService;
import com.siyuanren.gcocp.vo.QueryActiveVoucherVO;
import com.siyuanren.gcocp.vo.QueryVoucherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by   anxing
 * Project_name gcocp-parent
 * Path         com.siyuanren.gcocp.service.impl
 * Date         2018/6/8
 * Time         11:25
 * Description
 */
@Slf4j
@Service
public class UserVoucherServiceImpl implements UserVoucherService {
    @Autowired
    MyWxUserVoucherMapper userVoucherMapper;


    @Override
    public List<WxUserVoucher> getVoucherForCourse(QueryVoucherVO voucherVO) {
        WxUserVoucherExample example = new WxUserVoucherExample();
        example.or().andUserIdEqualTo(voucherVO.getUserId())
                .andVoucherCourseIdEqualTo(voucherVO.getCourseId())
                .andVoucherOverdueDateGreaterThanOrEqualTo(new Date())
                .andAvailableEqualTo(true);
        example.setOrderByClause("voucher_fee desc");

        List<WxUserVoucher> wxUserVouchers = userVoucherMapper.selectByExample(example);
        return wxUserVouchers;
    }

    @Override
    public List<WxUserVoucher> getNormalVoucher(Integer userId) {
        WxUserVoucherExample example = new WxUserVoucherExample();
        example.or().andUserIdEqualTo(userId).andVoucherStateEqualTo(VoucherStateEnum.VOUCHER_STATE_NOT_USED.getValue())
                .andAvailableEqualTo(true);

        List<WxUserVoucher> wxUserVouchers = userVoucherMapper.selectByExample(example);
        return wxUserVouchers;
    }

    @Override
    public List<WxUserVoucher> getOverdueVoucher(Integer userId) {
        List<Integer> status = new ArrayList<>();
        status.add(VoucherStateEnum.VOUCHER_STATE_OVERDUE.getValue()); // 已过期
        status.add(VoucherStateEnum.VOUCHER_STATE_YET_USED.getValue()); // 已使用
        WxUserVoucherExample example = new WxUserVoucherExample();
        example.or().andUserIdEqualTo(userId).andVoucherStateIn(status)
                .andAvailableEqualTo(true);

        List<WxUserVoucher> wxUserVouchers = userVoucherMapper.selectByExample(example);
        return wxUserVouchers;
    }

    @Override
    public boolean isFirstUseVoucher(Integer userId) {
        WxUserVoucherExample example = new WxUserVoucherExample();
        example.or().andUserIdEqualTo(userId).andVoucherStateEqualTo(VoucherStateEnum.VOUCHER_STATE_YET_USED.getValue())
                .andAvailableEqualTo(true);
        List<WxUserVoucher> wxUserVouchers = userVoucherMapper.selectByExample(example);
        if (wxUserVouchers.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
